import {_decorator, Component, director, EventTouch, instantiate, Layers, Layout, Node, Prefab} from "cc";
import {NormalBuilding} from "db://assets/script/game/city/normal-building";
import {GameEvent} from "db://assets/script/game-event";
import {CityBuildingTryPut} from "db://assets/script/common/events/city-events/city-building-try-put";
import {NormalBuildingStatus} from "db://assets/script/game/constants/normal-building-status";
import {configsHelper} from "db://assets/script/configs-helper";
import {buildingConfigsLoader} from "db://assets/script/configs/building-configs-loader";
import {userInfo} from "db://assets/script/user-info";
import {lobby} from "db://assets/script/lobby";

const {ccclass, property} = _decorator;

@ccclass('BuildingBox')
export class BuildingBox extends Component {

    @property(Layout)
    box!: Layout;

    @property(Prefab)
    buildingAsset!: Prefab

    start() {
        this.onEvent()
        this.updateUI()
    }

    onEvent() {
        director.on(GameEvent.userLevelUp, this.updateUI, this)
    }

    offEvent() {
        director.off(GameEvent.userLevelUp, this.updateUI, this)
    }

    updateUI() {
        const bcs = buildingConfigsLoader.findCanBuildBuildingConfigs()
        const limitedBuilding: Node[] = []
        const level = userInfo.loadUserLevel()
        for (let bc of bcs) {
            console.log('try show building in box:', bc.buildingId.toString())
            const building = instantiate(this.buildingAsset)
            building.getComponent(NormalBuilding)!.loadBuilding(bc.buildingId, null, null)
            building.getComponent(NormalBuilding)!.changeStatus(NormalBuildingStatus.show)
            building.layer = Layers.Enum.UI_2D
            building.getComponent(NormalBuilding)!.building.node.layer = Layers.Enum.UI_2D
            const numLimit = configsHelper.loadUserLevelBuildingNumLimit(level, bc.buildingId)
            const bn = userInfo.cityInfo.loadBuildingsNum(1, bc.buildingId)
            building.on(Node.EventType.TOUCH_END, async (event: EventTouch) => {
                // 阻止事件继续冒泡
                event.propagationStopped = true
                console.log('click building in box', bc.buildingId)
                const wn = userInfo.cityInfo.loadBuildings(1).filter(it => it.completeAtMs.gt(0)).length
                const lc = configsHelper.loadUserLevelConfig(level)!
                if (lc.workerNum - wn == 0) {
                    lobby.instance.showToast('工匠数量不足')
                    return
                }
                if (bn >= numLimit) {
                    lobby.instance.showToast('建筑数量达到上限')
                    return
                }
                director.emit(GameEvent.cityBuildingTryPut, new CityBuildingTryPut(bc.buildingId))
            })

            if (bn >= numLimit) limitedBuilding.push(building)
            else this.box.node.addChild(building)
        }
        limitedBuilding.forEach((item) => this.box.node.addChild(item))
    }

    protected onDestroy() {
        this.offEvent()
    }
}